﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Text;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Openfree.Framework.DataAccess;

public partial class TaskAccept : PMSBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string cmdText = "select a.*,case when B.status is null then '" + PMSGoodsState.NotOut + @"' else B.status end ReqStatus, (select Username from T_User where UserID=a.UserID) Username from PMS_TaskLX a left join IC_Requisition B on B.TaskID=a.TaskID order by a.CreateDate desc";
            FillGirdView(0, cmdText);
        }
        else
        {
            FillGirdView(this.gridView.SplitPageIndex, string.Empty);
        }
    }

    protected void FillGirdView(int currentPage, string cmdText)
    {
        if (string.IsNullOrEmpty(cmdText))
        {
            cmdText = ViewState[this.ClientID + "cmdText"].ToString();
        }
        ViewState[this.ClientID + "cmdText"] = cmdText;
        SqlSplitPage splitPage = new SqlSplitPage(base.Connection, cmdText);
        splitPage.PageSize = 10;
        DataSet ds = splitPage.LoadDataSet(currentPage);
        gridView.ShowFooter = true;
        gridView.NavigationData = splitPage.GetNavigationData(currentPage);
        gridView.DataSource = ds.Tables[0];
        gridView.DataBind();
    } 

    protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[6].Text = PMSTaskState.GetName(e.Row.Cells[6].Text);
            e.Row.Cells[7].Text = PMSGoodsState.GetName(e.Row.Cells[7].Text);
        }
    }

    protected void gridView_SplitPageIndexChange(object sender, CommandEventArgs e)
    {
        FillGirdView(Int32.Parse(e.CommandArgument.ToString()), ViewState[this.ClientID + "cmdText"].ToString());
    }

    private void ClearValue()
    {
        tbGuest.Text = "";
        tbLocation.Text = "";
        tbPhone.Text = "";
        tbTroubleDescription.Text = "";
        lbUserID.Text = SessionManager.Username;
        lbOrgID.Text = SessionManager.OrgName;
        lbCreateDate.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm");
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        hfAddOrEdit.Value = "Add";
        hfTaskID.Value = string.Empty;
        pnlGrid.Visible = false;
        pnlEdit.Visible = true;
        ClearValue();
    }

    protected void btnEdit_Click(object sender, EventArgs e)
    {
        string taskID = Request["TaskID"];
        if (string.IsNullOrEmpty(taskID))
        {
            base.Alert("请选择一条记录");
            FillGirdView(this.gridView.SplitPageIndex, string.Empty);
            return;
        }

        hfAddOrEdit.Value = "Edit";
        hfTaskID.Value = taskID;

        string cmdText = "select * from PMS_TaskLX where TaskID='{0}'";
        cmdText = string.Format(cmdText, taskID);

        DataSet ds = BaseAccess.GetDataSet(base.Connection, cmdText);

        if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
        {
            Alert("您选择的记录已经被删除，请刷新数据！");
            return;
        }
        DataRow row = ds.Tables[0].Rows[0];
        if (Convert.ToInt32(row["TaskState"]) == PMSTaskState.Complete)
        {
            Alert("您选择的任务已经完成，不能再进行操作！");
            return;
        } 
        pnlGrid.Visible = false;
        pnlEdit.Visible = true;
        ClearValue(); 
        UserAccess user = new UserAccess(base.Connection);
        tbGuest.Text = row["Guest"].ToString();
        tbLocation.Text = row["Location"].ToString();
        tbPhone.Text = row["Phone"].ToString();
        tbTroubleDescription.Text = row["TroubleDescription"].ToString();
        lbOrgID.Text = user.GetUserOrgName(row["OrgID"].ToString());
        lbUserID.Text = user.GetUserNameByUserID(row["UserID"].ToString());
        lbCreateDate.Text = Convert.ToDateTime(row["CreateDate"].ToString()).ToString("yyyy-MM-dd HH:mm");
     
    }

    protected void btnRemove_Click(object sender, EventArgs e)
    {
        string taskID = Request["TaskID"];
        if (string.IsNullOrEmpty(taskID))
        {
            base.Alert("请选择一条记录");
            FillGirdView(this.gridView.SplitPageIndex, string.Empty);
            return;
        }

        string cmdText = "select * from PMS_TaskLX where TaskID='{0}'";
        cmdText = string.Format(cmdText, taskID);

        DataSet ds = BaseAccess.GetDataSet(base.Connection, cmdText);

        if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
        {
            Alert("您选择的记录已经被删除，请刷新数据！");
            return;
        }
        DataRow row = ds.Tables[0].Rows[0];
        if (Convert.ToInt32(row["TaskState"]) == PMSTaskState.Complete)
        {
            Alert("您选择的任务已经完成，不能再进行作废操作！");
            return;
        } 
        cmdText = @"delete from PMS_TaskLX where TaskID='{0}';
                        delete from PMS_TaskUser where TaskID='{0}';
                        delete from IC_RequisitionItem where RequisitionID in (select RequisitionID from IC_Requisition where TaskID='{0}');
                        delete from IC_Requisition where TaskID='{0}';";
        cmdText = string.Format(cmdText, taskID);
        BaseAccess.ExecuteNonQuery(base.Connection, cmdText);
        FillGirdView(this.gridView.SplitPageIndex, string.Empty);
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (hfAddOrEdit.Value == "Add")
        {
            string cmdText = "select * from PMS_TaskLX where 1=2";
            DataSet ds = BaseAccess.GetDataSet(base.Connection, cmdText, "PMS_TaskLX");
            DataRow row = ds.Tables[0].NewRow();
            row["TaskID"] = Guid.NewGuid().ToString();
            //row["TaskCode"] =
            row["Guest"] = tbGuest.Text.Trim();
            row["Location"] = tbLocation.Text.Trim();
            row["Phone"] = tbPhone.Text.Trim();
            row["TroubleDescription"] = tbTroubleDescription.Text.Trim();
            row["UserID"] = SessionManager.UserID;
            row["OrgID"] = SessionManager.OrgID;
            row["CreateDate"] = DateTime.Now.ToString();
            row["GoodsState"] = PMSGoodsState.NotOut;
            row["TaskState"] = PMSTaskState.Running;
            row["SourceState"] = PMSSourceState.From123;
            ds.Tables[0].Rows.Add(row);

            BaseAccess.UpdateDataSet(base.Connection, "PMS_TaskLX", ds);
            ClearValue();
            base.Alert("保存成功");
        }
        else
        {
            string cmdText = "select * from PMS_TaskLX where TaskID='{0}'";
            cmdText = string.Format(cmdText, hfTaskID.Value);
            DataSet ds = BaseAccess.GetDataSet(base.Connection, cmdText, "PMS_TaskLX");
            DataRow row = ds.Tables[0].Rows[0];
            row["Guest"] = tbGuest.Text.Trim();
            row["Location"] = tbLocation.Text.Trim();
            row["Phone"] = tbPhone.Text.Trim();
            row["TroubleDescription"] = tbTroubleDescription.Text.Trim();
            BaseAccess.UpdateDataSet(base.Connection, "PMS_TaskLX", ds);

            pnlGrid.Visible = true;
            pnlEdit.Visible = false;
            FillGirdView(this.gridView.SplitPageIndex, string.Empty);
            base.Alert("保存成功");
        }

    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        pnlGrid.Visible = true;
        pnlEdit.Visible = false;
        FillGirdView(this.gridView.SplitPageIndex, string.Empty);
    }
}
